/**
  HomeMade by shoe[box]
 
  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
  
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the 
    documentation and/or other materials provided with the distribution.
  
  * Neither the name of shoe[box] nor the names of its 
    contributors may be used to endorse or promote products derived from 
    this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.shoebox.guacamole.view {

	import org.shoebox.guacamole.materials.BitmapMaterial;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.VertexBuffer3D;
	import org.shoebox.guacamole.interfaces.IRenderable;
	import org.shoebox.guacamole.materials.abstracts.AMaterial;
	import org.shoebox.guacamole.view.abstracts.GuaInteractive;
	import org.shoebox.utils.logger.Logger;
	
	/**
	* org.shoebox.guacamole.view.MoleSprite
	* @author shoebox
	*/
	public class GuaSprite extends GuaInteractive implements IRenderable{
		
		public var vIndexBuffer		: IndexBuffer3D;
		public var vBuffer				: VertexBuffer3D;
		
		public var invalidate			: Boolean = false;
		
		protected var _iTessX			: int = 1;
		protected var _iTessY			: int = 1;
		protected var _bInit			: Boolean = false;
		
		// -------o constructor
		
			public function GuaSprite( width : int , height : int , material : AMaterial , tessX : int = 1 , tessY : int = 1 ) : void {
				
				super( );
				setScale( 1 , 1 );
				setSize( width , height );	
				setMaterial( material );
				initialize( tessX , tessY );
			}

		// -------o public
			
			/**
			* initialize function
			* @public
			* @param 
			* @return
			*/
			final public function initialize( tessX : int , tessY : int ) : void {
			
				_iTessX = tessX;
				_iTessY = tessY;
				_updateMesh( );
				
			}
			
			/**
			* getMaterial function
			* @public
			* @param 
			* @return
			*/
			final public function getMaterial() : AMaterial {
				return material;
			}
			
			/**
			* getIndicesIndexBuffer3D function
			* @public
			* @param 
			* @return
			*/
			final public function getIndices() : IndexBuffer3D {
				return vIndexBuffer;			
			}
			
			/**
			* clone function
			* @public
			* @param 
			* @return
			*/
			final public function clone() : GuaSprite {
				var 	sp : GuaSprite = new GuaSprite( _iWidth , _iHeight , material );
						sp.setIndexs( vIndexs );
						sp.setVertices( vVerticesCoords , bufferFormatVertices );
						sp.setUvs( vUVCoords , bufferFormatUV );
						sp.setNormals( _vNormalsCoords , bufferFormatNormals );
				return sp;
			}
			
			/**
			* setSize function
			* @public
			* @param 
			* @return
			*/
			override public function setSize( w : int , h : int ) : void {
				super.setSize( w , h );
				_updateMesh( );
			}
			
		// -------o protected
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _updateMesh( ) : void {
				var tw : uint = _iTessX + 1;
				var numVerts : uint = ( _iTessY + 1) * tw;
				
				
				var vVertices 	: Vector.<Number> 	= new Vector.<Number>( numVerts * 3 , true );
				var vUvs	 	: Vector.<Number> 	= new Vector.<Number>( numVerts * 3 , true );
				var vIndices 	: Vector.<uint> 		= new Vector.<uint>( _iTessX * _iTessY * 6, true);
				
				var nUFactor : Number = 1;
				var nVFactor : Number = 1;
				if( material is BitmapMaterial ){
						
						var mat : BitmapMaterial = material as BitmapMaterial;
						if( mat.bRepeat ){
							nUFactor = _iWidth / mat.sourceWidth;
							nVFactor = _iHeight / mat.sourceHeight;
						
						}
						
				}
				
				numVerts = 0;
				var dy : int = 0 , dx : int , numInds : int = 0 , base : uint , tx : Number , ty : Number;
	
				for ( dy ; dy <= _iTessY; ++dy) {
					
					dx = 0;
					
					for ( dx ; dx <= _iTessX ; ++dx) {
						
						tx = dx / _iTessX;
						ty = dy / _iTessY;
	
						vUvs[numVerts] = tx * nUFactor;
						vVertices[numVerts] = tx * getWidth( );
						numVerts++;
						
						
						vUvs[numVerts] = ty * nVFactor;
						vVertices[numVerts] = ty * getHeight( );
						numVerts++;
						
						vUvs[numVerts] = 0;
						vVertices[numVerts] = 1;
						numVerts++;
						
						if (dx != _iTessX && dy !== _iTessY) {
							base = dx + dy * tw;
							vIndices[numInds++] = base;
							vIndices[numInds++] = base + tw;
							vIndices[numInds++] = base + tw + 1;
							vIndices[numInds++] = base;
							vIndices[numInds++] = base + tw + 1;
							vIndices[numInds++] = base + 1;
						}
					}
				}
				
				
				
				setIndexs( vIndices );
				setVertices( vVertices 	, Context3DVertexBufferFormat.FLOAT_3 );
				setUvs( vUvs 			, Context3DVertexBufferFormat.FLOAT_3 );
				
			}
			
		// -------o misc

			public static function trc( ...args : Array ) : void {
				Logger.log(GuaSprite, args);
			}
	
			
	}
}
